﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StoneStorage
{
    public partial class MainForm : Form
    {
        private StoneStorageDataSet dSet;

        private BindingSource storageBSource = new BindingSource();

        private Repository repository;

        private Filter filter = new Filter();

        private BindingSource filterBSource = new BindingSource();

        public MainForm()
        {
            InitializeComponent();
            this.repository = new Repository();
            dSet = repository.DSet;

            this.LoadToDgvStorage();
            this.InitDgvFilter();
        }

        private void LoadToDgvStorage()
        {
            storageBSource.DataSource = dSet.Storage;
            dgvStorage.DataSource = storageBSource;
            storageBSource.Filter = dSet.Storage.CountColumn.ColumnName + "<>0";
            dgvStorage.ReadOnly = true;
            dgvStorage.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvStorage.AllowUserToAddRows = false;
            dgvStorage.Columns[dSet.Storage.IdColumn.ColumnName].Visible = false;

            var column1 = dSet.Storage.StoneIdColumn.ColumnName;
            dgvStorage.Columns[column1].HeaderText = "Вид камня";
            dgvStorage.Columns[column1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            var column2 = dSet.Storage.ProducerIdColumn.ColumnName;
            dgvStorage.Columns[column2].HeaderText = "Производитель";
            dgvStorage.Columns[column2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            dgvStorage.Columns[dSet.Storage.StoneLengthColumn.ColumnName].HeaderText = "Длина, см.";
            dgvStorage.Columns[dSet.Storage.StoneWidthColumn.ColumnName].HeaderText = "Ширина, см.";
            dgvStorage.Columns[dSet.Storage.StoneHeightColumn.ColumnName].HeaderText = "Высота, см.";
            dgvStorage.Columns[dSet.Storage.CountColumn.ColumnName].HeaderText = "Количество, шт.";
        }

        private void InitDgvFilter()
        {
            dgvFilter.AutoGenerateColumns = false;

            filterBSource.DataSource = filter;
            dgvFilter.DataSource = filterBSource;

            BindingSource stoneBSource = new BindingSource();
            BindingSource producerBSource = new BindingSource();

            stoneBSource.DataSource = dSet.StoneDictionary;
            producerBSource.DataSource = dSet.ProducerDictionary;

            // Создание колонки Вид камня

            DataGridViewComboBoxColumn comboColumn1 = new DataGridViewComboBoxColumn();
            comboColumn1.HeaderText = "Вид камня";
            comboColumn1.Name = "Stone";
            comboColumn1.DataSource = stoneBSource;
            comboColumn1.DataPropertyName = "StoneId";
            comboColumn1.ValueMember = "Id";
            comboColumn1.DisplayMember = "Name";
            comboColumn1.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvFilter.Columns.Add(comboColumn1);


            // Создание колонки Производитель

            DataGridViewComboBoxColumn comboColumn2 = new DataGridViewComboBoxColumn();
            comboColumn2.HeaderText = "Производитель";
            comboColumn2.Name = "Producer";
            comboColumn2.DataSource = producerBSource;
            comboColumn2.DataPropertyName = "ProducerId";
            comboColumn2.ValueMember = "Id";
            comboColumn2.DisplayMember = "Name";
            comboColumn2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvFilter.Columns.Add(comboColumn2);

            // Создание колонки Длина

            DataGridViewTextBoxColumn length = new DataGridViewTextBoxColumn();
            length.Name = "Length";
            length.HeaderText = "Длина, см.";
            length.DataPropertyName = "Length";
            dgvFilter.Columns.Add(length);

            // Создание колонки Ширина

            DataGridViewTextBoxColumn width = new DataGridViewTextBoxColumn();
            width.Name = "Width";
            width.HeaderText = "Ширина, см.";
            width.DataPropertyName = "Width";
            dgvFilter.Columns.Add(width);

            // Создание колонки Высота

            DataGridViewTextBoxColumn height = new DataGridViewTextBoxColumn();
            height.Name = "Height";
            height.HeaderText = "Высота, см.";
            height.DataPropertyName = "Height";
            dgvFilter.Columns.Add(height);

            foreach (DataGridViewColumn column in dgvFilter.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        private void tsmiStoneDictionary_Click(object sender, EventArgs e)
        {
            this.EditDictionary(dSet.StoneDictionary, dSet.StoneDictionary.NameColumn, "Вид камня");
        }

        private void tsmiProducerDictionary_Click_1(object sender, EventArgs e)
        {
            this.EditDictionary(dSet.ProducerDictionary, dSet.ProducerDictionary.NameColumn, "Производитель");
        }

        private void EditDictionary(DataTable table, DataColumn column, string caption)
        {
            DictionaryForm form = new DictionaryForm(table, column, caption);
            if (form.ShowDialog() == DialogResult.OK)
            {
                repository.Save();
                return;
            }
            table.RejectChanges();
        }

        private void tsmiAddIncomeInvoice_Click(object sender, EventArgs e)
        {
            InvoiceForm form = new InvoiceForm(dSet, InvoiceKindEnum.Income);
            if (form.ShowDialog() == DialogResult.OK)
            {
                BusinessRules bRules = new BusinessRules(dSet);
                bRules.AddIncomeInvoice(form.Invoice);
                storageBSource.DataSource = dSet.Storage;
                repository.Save();
            }
        }

        private void tsmiAddExpenditureInvoice_Click(object sender, EventArgs e)
        {
            InvoiceForm form = new InvoiceForm(dSet, InvoiceKindEnum.Expenditure);
            if (form.ShowDialog() == DialogResult.OK)
            {
                BusinessRules bRules = new BusinessRules(dSet);
                bRules.AddExpenditureInvoice(form.Invoice);
                storageBSource.DataSource = dSet.Storage;
                repository.Save();
            }
        }

        private void tsmiDeleteIncomeInvoice_Click(object sender, EventArgs e)
        {
            DeleteInvoiceForm form = new DeleteInvoiceForm(dSet, InvoiceKindEnum.Income);
            if (form.ShowDialog() == DialogResult.OK)
            {
                var bRules = new BusinessRules(dSet);
                bRules.RemoveIncomeInvoice(form.Invoice.Number, form.Invoice.Date);
                storageBSource.DataSource = dSet.Storage;
                repository.Save();
            }
        }

        private void tsmiDeleteExpenditureInvoice_Click(object sender, EventArgs e)
        {
            DeleteInvoiceForm form = new DeleteInvoiceForm(dSet, InvoiceKindEnum.Expenditure);
            if (form.ShowDialog() == DialogResult.OK)
            {
                var bRules = new BusinessRules(dSet);
                bRules.RemoveExpenditureInvoice(form.Invoice.Number, form.Invoice.Date);
                storageBSource.DataSource = dSet.Storage;
                repository.Save();
            }
        }

        private void dgvStorage_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value == null)
            {
                return;
            }

            if (dgvStorage.Columns[e.ColumnIndex].Name == dSet.Storage.StoneIdColumn.ColumnName)
            {
                e.Value = dSet.StoneDictionary.FindById((int)e.Value).Name;
            }

            if (dgvStorage.Columns[e.ColumnIndex].Name == dSet.Storage.ProducerIdColumn.ColumnName)
            {
                e.Value = dSet.ProducerDictionary.FindById((int)e.Value).Name;
            }

        }

        private void bDeleteFilter_Click(object sender, EventArgs e)
        {
            filter.FilterZiroOut();

            filterBSource.DataSource = filter;
            
            dgvFilter.Refresh();
            storageBSource.Filter = dSet.Storage.CountColumn.ColumnName + "<>0";
        }

        private void dgvFilter_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvFilter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
            {
                return;
            }

            storageBSource.Filter = filter.CreateFilterString() + " and " 
                + dSet.Storage.CountColumn.ColumnName + "<>0";
        }
    }
}
